import request from '@/utils/request.js'

// 注册接口
export const registerAPI = (form) => { // form是一个对象
  // 返回的是promise对象
  return request({
    url: '/api/reg',
    method: 'POST',
    data: {
      username: form.username,
      password: form.password,
      repassword: form.repassword
    }
  })
}
/**
 *登录接口
 * @param {*} loginRef {username,password}
 * @returns promise对象
 */
export const loginAPI = (loginRef) => { // form是一个对象
  // 返回的是promise对象
  return request({
    url: '/api/login',
    method: 'POST',
    data: {
      username: loginRef.username,
      password: loginRef.password
    }
  })
}
/**
 * 用户信息请求接口
 * @returns userInfo
 */
export const getUserInfoAPI = () => {
  return request({
    url: '/my/userinfo',
    method: 'GET'
    // 传参给后台：params(查询字符串query),data(请求体body),headers(请求头)
    // headers: {
    //   Authorization: store.state.token
    // }
  })
}
/**
 * 动态生成侧边栏
 * @returns data对象
 */
export const getMenusListAPI = () => {
  return request({
    url: '/my/menus'
    // headers: {
    //   Authorization: store.state.token
    // }
  })
}
/**
 * 更新个人信息
 * @param {*} userFrom 对象
 * @returns 对象
 */
export const updateUserInfoAPI = (userFrom) => {
  return request({
    url: '/my/userinfo',
    method: 'PUT',
    data: {
      id: userFrom.id,
      username: userFrom.username,
      nickname: userFrom.nickname,
      email: userFrom.email,
      user_pic: userFrom.user_pic
    }
  })
}
/**
 * 更新用户头像
 * @returns promise 对象
 */
export const updateAvatarAPI = (avatar) => {
  return request({
    url: '/my/update/avatar',
    method: 'patch',
    data: {
      avatar
    }
  })
}
/**
 * 更改密码接口
 * @param {对象} pwdForm
 * @returns 对象
 */
export const updatePwdAPI = (pwdForm) => {
  return request({
    url: '/my/updatepwd',
    method: 'patch',
    data: {
      old_pwd: pwdForm.old_pwd,
      new_pwd: pwdForm.new_pwd,
      re_pwd: pwdForm.re_pwd

    }
  })
}
/**
 * 获得文章分类列表
 * @returns promise对象
 */
export const getArtCateListAPI = () => {
  return request({
    url: '/my/cate/list'
  })
}
/**
 * 添加文章分类接口
 * @param {对象} addForm
 * @returns promise对象
 */
export const addArtCateAPI = (addForm) => {
  return request({
    url: '/my/cate/add',
    method: 'post',
    data: {
      cate_name: addForm.cate_name,
      cate_alias: addForm.cate_alias
    }
  })
}
/**
 * 更新-文章分类
 * @param {*} param0 { id: 文章分类id, cate_name: 文章分类名字, cate_alias: 文章分类别名 }
 * @returns Promise对象
 */
export const updateArtCateAPI = ({ id, cate_name, cate_alias }) => {
  return request({
    url: '/my/cate/info',
    method: 'PUT',
    data: {
      id,
      cate_name,
      cate_alias
    }
  })
}
/**
 *删除文章分类接口
 * @param {*} id删除对应id
 * @returns promise对象
 */
export const delArtCateAPI = (id) => {
  return request({
    url: '/my/cate/del',
    method: 'delete',
    params: {
      id
    }
  })
}
/**
 * 传输表数据对象
 * @param {*} pubForm 表单数据
 * @returns Promise对象
 */
export const uploadArticleAPI = (pubForm) => {
  return request({
    url: '/my/article/add',
    method: 'post',
    data: pubForm
  })
}
/**
 * 获取文章列表
 * @param {*} param0 { pagenum: 当前页码数, pagesize: 当前页条数, cate_id: 文章分类id, state: 文章状态 }
 * @returns Promise对象
 */
export const getArticleListAPI = ({ pagenum, pagesize, cate_id, state }) => {
  return request({
    url: '/my/article/list',
    params: {
      pagenum,
      pagesize,
      cate_id,
      state
    }
  })
}
/**
 * 获取-文章详情
 * @param {*} id 文章id
 * @returns Promise对象
 */
export const getArticleDetailFn = (id) => {
  return request({
    url: '/my/article/info',
    params: {
      id
    }
  })
}
export const delArticleAPI = (id) => {
  return request({
    url: '/my/article/info',
    method: 'DELETE',
    params: {
      id
    }
  })
}
